//
//  jxsSelectDueDateViewController.m
//  ToDoList
//
// This code file is apart of project "iPhone-ToDo-List" 
// (http://code.google.com/p/iphone-todo-list/)
//
// Copyright (c) 2009 Frank Villasenor
//
// Distributed under the terms of the Lesser GNU General Public License.
// A copy of the license should be with these files. If not, you 
// can retrieve a copy from http://www.gnu.org/licenses/lgpl-3.0.txt
// 

#import "jxsSelectDueDateViewController.h"
#import "Constants.h"
#import "jxsEditTaskDetailViewController.h"
#import "tosuTask.h"

#import "jxsDateShortcutCalculationBase.h"
#import "jxsDateShortcutCalculationFactory.h"
//#import "jxsDateShortcutCalculationToday.h"
//#import "jxsDateShortcutCalculationTomorrow.h"
//#import "jxsDateShortcutCalculationOneWeek.h"
//#import "jxsDateShortcutCalculationTwoWeeks.h"
//#import "jxsDateShortcutCalculationOneMonth.h"
//#import "jxsDateShortcutCalculationSixMonths.h"
//#import "jxsDateShortcutCalculationOneYear.h"

//
// Define the Date Short Cut tag numbers
// by using the 'days' the button will add
// to the calendar. These are not exact
// and shouldn't be used in calculations.
#define kDateShortCutToday		0
#define kDateShortCutTomorrow	1
#define kDateShortCutOneWeek	7
#define kDateShortCutTwoWeeks	14
#define kDateShortCutOneMonth	31
#define kDateShortCutSixMonths	182
#define kDateShortCutOneYear	364


@implementation jxsSelectDueDateViewController

@synthesize selectedDueDateLabel, datePicker;
@synthesize selectedDate;
@synthesize containingNavController;
@synthesize longRangeToolBar, shortRangeToolBar;

//
// These are released right after use
// because they are not used after that.
@synthesize cancelButton, confirmButton;

- (void)dealloc 
{
	[selectedDueDateLabel release];
	[datePicker release];
	[selectedDate release];
	[containingNavController release];
	[longRangeToolBar release];
	[shortRangeToolBar release];
	
    [super dealloc];
}

/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad 
{	
	[cancelButton stylizeButtonWithStockCustomImages];
	[confirmButton stylizeButtonWithStockCustomImages];
	
	//
	// These won't be used any longer so release them...
	[cancelButton release];
	[confirmButton release];
	
    [super viewDidLoad];
}

//
//Sent to the controller before the view appears and any animations begin.
- (void)viewWillAppear:(BOOL)animated
{
	if( self.containingNavController == nil )
	{
		[NSException raise:@"The containing Nav controller must be set!" format:@"The containing Nav controller must be set! Can not proceed."]; 
	}
	else
	{
		[self.containingNavController setNavigationBarHidden:YES animated:animated];
	}
	
	self.selectedDueDateLabel.text = [selectedDate getShortDateWithoutTime];
	[self.datePicker setDate:selectedDate animated:animated];
	
	//
	// Set the current date in to the label.
	self.selectedDueDateLabel.text = [self.selectedDate getShortDateWithoutTime];
	
	[super viewWillAppear:animated];
}

//
//Sent to the controller after the view fully appears and animations end.
- (void)viewDidAppear:(BOOL)animated
{
	[super viewDidAppear:animated];
}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning 
{
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}

#pragma mark -
#pragma mark Custom ViewController Methods

//
// TODO: Finish completing.
- (IBAction)confirmedSelectionWithButton:(UIButton *)button
{
	self.selectedDate = datePicker.date;
	
	//
	//Call the cancel method because it does what we need.
	// (this is done in place of poping the controller which is
	// done in this method)
	[self canceledSelectionWithButton:button];
	
	//
	//This must be id and not UIViewController *, otherwise a warning is issued...
	id topViewController = [self.containingNavController topViewController]; 
	if( [topViewController conformsToProtocol:@protocol(jxsDueDateReceiver)] )
	{
		//
		// Programming in Objective-C 2.0; Pg. 233.
		// We've checked to  ensure we conform to the protocal so we can
		// now send messages inside that protocal (but there is only one option)
		[topViewController setDueDateToDate:self.selectedDate]; 
	}
	else
	{
		[NSException raise:@"jxsDueDateReceive Protocal not conformed to"  
			format:@"the topViewController must conform to the jxsDueDateReceiver protocal."];
	} 
}

- (IBAction)canceledSelectionWithButton:(UIButton *)button
{
	//
	// Leave self.selectedDate as is then pop this off the stack.
	[self.containingNavController popViewControllerAnimated:YES];
	[self.containingNavController setNavigationBarHidden:NO animated:YES]; //Reshow the nav bar.
	return;
}

- (IBAction)selectedDateShortCut:(id)sender
{
	if( [sender isKindOfClass:[UIBarButtonItem class]] )
	{
		UIBarButtonItem *button = (UIBarButtonItem *)sender;
		NSInteger tagNum = button.tag;
		
		// Use something similar to the state/straty pattern.
		//Declare a abstract var here
		NSObject <jxsDateShortcutCalculationBase> *calculation = nil;
		
		switch( tagNum )  //The switch finds the approriate object
		{
			case kDateShortCutToday:
				calculation = [jxsDateShortcutCalculationFactory today];
				break;
			case kDateShortCutTomorrow:
				calculation = [jxsDateShortcutCalculationFactory tomorrow];
				break;
			case kDateShortCutOneWeek:
				calculation = [jxsDateShortcutCalculationFactory oneWeek];
				break;
			case kDateShortCutTwoWeeks:
				calculation = [jxsDateShortcutCalculationFactory twoWeeks];
				break;
			case kDateShortCutOneMonth:
				calculation = [jxsDateShortcutCalculationFactory oneMonth];
				break;
			case kDateShortCutSixMonths:
				calculation = [jxsDateShortcutCalculationFactory sixMonths];
				break;
			case kDateShortCutOneYear:
				calculation = [jxsDateShortcutCalculationFactory oneYear];
				break;
				
			default:
				break;
		}
		
		//Use that object to find how far in advance
		// the return value should be.
		NSDate *localSelectedDate = [calculation calculateDate];
		[datePicker setDate:localSelectedDate animated:YES];
		
		[calculation release];
	}
}


@end
